package com.aries.ui.view.title.util;

import ohos.agp.components.Component;
import ohos.agp.components.ComponentContainer;
import ohos.agp.utils.Matrix;
import ohos.agp.utils.Rect;
import ohos.agp.utils.RectFloat;

/**
 * @Author: AriesHoo on 2018/7/19 10:39
 * @E-Mail: AriesHoo@126.com
 * Function: 
 * Description: 
 */
public class ViewGroupUtilsHoneycomb {

    private static final ThreadLocal<Matrix> sMatrix = new ThreadLocal<>();
    private static final ThreadLocal<RectFloat> sRectF = new ThreadLocal<>();

    public static void offsetDescendantRect(ComponentContainer group, Component child, Rect rect) {
        Matrix m = sMatrix.get();
        if (m == null) {
            m = new Matrix();
            sMatrix.set(m);
        } else {
            m.reset();
        }

        offsetDescendantMatrix(group, child, m);

        RectFloat rectF = sRectF.get();
        if (rectF == null) {
            rectF = new RectFloat();
            sRectF.set(rectF);
        }
        rectF.left = rect.left;
        rectF.top = rect.top;
        rectF.right = rect.right;
        rectF.bottom = rect.bottom;
        m.mapRect(rectF);
        rect.set((int) ((double)rectF.left +(double) 0.5f), (int) ((double)rectF.top + (double)0.5f),
                (int) ((double)rectF.right + (double)0.5f), (int) ((double)rectF.bottom + (double)0.5f));
    }

    static void offsetDescendantMatrix(ComponentContainer target, Component view, Matrix m) {
        final ComponentContainer parent = (ComponentContainer) view.getComponentParent();
        if (parent instanceof Component && parent != target) {
            final Component vp = (Component) parent;
            offsetDescendantMatrix(target, vp, m);
            m.preTranslate(-vp.getScrollValue(Component.AXIS_X), -vp.getScrollValue(Component.AXIS_Y));
        }

        m.preTranslate(view.getLeft(), view.getTop());

//        if (!view.getMatrix().isIdentity()) {
//            m.preConcat(view.getMatrix());
//        }
    }
}
